package com.xqboss.apps.service.consumer;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.xqboss.apps.domain.consumer.ConsumerInviteRecord;
import com.xqboss.common.constant.CacheConstants;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.core.domain.entity.SysUser;
import com.xqboss.common.core.domain.model.LoginBody;
import com.xqboss.common.core.redis.RedisCache;
import com.xqboss.common.utils.SecurityUtils;
import com.xqboss.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author Administrator
 */
@Service
public class ConsumerService {

    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private ConsumerInviteRecordService consumerInviteRecordService;
    @Autowired
    private ConsumerTeamService consumerTeamService;
    @Autowired
    private ConsumerTeamMemberService consumerTeamMemberService;
    @Autowired
    private ConsumerRealNameService consumerRealNameService;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 绑定邀请人
     *
     * @param invitationCode 邀请码
     * @return
     */
    public R bindInvite(Long userId, String invitationCode) {
        if (userId == null) {
            return R.fail("用户不存在，参数错误");
        }
        SysUser inviteUser = userMapper.selectUserByInvitationCode(invitationCode);
        if (ObjUtil.isNull(inviteUser)) {
            return R.fail("邀请人不存在");
        }
        if (inviteUser.getUserId().equals(userId)) {
            return R.fail("不能绑定自己");
        }
        if (inviteUser.getInviterId() != null && inviteUser.getInviterId().equals(userId)) {
            return R.fail("不能交叉绑定");
        }
        SysUser user = userMapper.selectUserById(userId);
        if (ObjUtil.isNotNull(user.getInviterId())) {
            return R.fail("已经绑定了用户");
        }
        SysUser updateUser = new SysUser();
        updateUser.setUserId(user.getUserId());
        updateUser.setInviterId(inviteUser.getUserId());
        userMapper.updateUser(updateUser);
        //新增邀请记录
        ConsumerInviteRecord record = new ConsumerInviteRecord();
        record.setUserId(user.getUserId());
        record.setNickName(user.getNickName());
        record.setInvitId(inviteUser.getUserId());
        record.setPhone(user.getPhonenumber());
        consumerInviteRecordService.save(record);
        return R.ok();
    }

    /**
     * 注销用户
     * @param userId
     * @param params
     * @return
     */
    public R v2Logout(Long userId, LoginBody params) {
        if (Objects.isNull(params) || StrUtil.isBlank(params.getPassword())) {
            return R.fail("密码不能为空");
        }
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        //校验用户密码是否正确
        if (!SecurityUtils.matchesPassword(params.getPassword(), sysUser.getPassword())) {
            return R.fail("密码错误");
        }
        //注销用户 清除用户token
        redisCache.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + userId);

        String number = RandomUtil.randomNumbers(6);
        sysUserMapper.logoutUser(userId, "_" + number);
        //清除实名信息
        consumerRealNameService.deleteByUserId(userId);
        //清除队伍
        Long teamId = consumerTeamMemberService.getTeamIdByUserId(userId);
        if (Objects.nonNull(teamId)) {
            consumerTeamService.quit(Math.toIntExact(teamId), userId);
        }
        return R.ok_msg("注销成功");
    }
}
